package com.shangqing.system.windinfomation.service.Impl;

import com.shangqing.system.droughtinformation.entity.City;
import com.shangqing.system.windinfomation.entity.WindGrade;
import com.shangqing.system.windinfomation.entity.WindWarning;
import com.shangqing.system.windinfomation.entity.Windinformation;
import com.shangqing.system.windinfomation.mapper.WindInformationMapper;
import com.shangqing.system.windinfomation.service.IwindInfomationSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class WindinfomationImpl implements IwindInfomationSercice {

    @Autowired
    private WindInformationMapper windInformationMapper;
    @Override
    public List<Windinformation> getAllInfo() {
        List<Windinformation> windinformationList=new ArrayList<>();
        List<Windinformation> Windinformation=windInformationMapper.selectAll();
        City city=null;
        for (Windinformation w : Windinformation) {
            w.setWindGrade(windInformationMapper.getWindGradeById(w.getWindgradeid()));
            w.setWindWarning(windInformationMapper.getWindwarningByID(w.getWindwarninglevelid()));
            city = windInformationMapper.getAllCity(w.getLocationid());
            city.setCityList(cityVos(city));
            w.setCity(city);
            windinformationList.add(w);
        }
        return windinformationList;
    }
    /**
     * 递归方法体
     */
    private City cityVos(City city) {
        if (city.getParentId() == 0) {
            return null;
        } else {
            City vo = windInformationMapper.getAllCity(city.getParentId());
            City cityVo = new City();
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCityList(cityVos(vo));
            return cityVo;
        }
    }

    @Override
    public City getAllCity(int id) {
        return windInformationMapper.getAllCity(id);
    }

    @Override
    public List<WindGrade> getWindGradeById(int id) {
        return (List<WindGrade>) windInformationMapper.getWindGradeById(id);
    }

    @Override
    public List<WindWarning> getWindWarningById(int id) {
        return (List<WindWarning>) windInformationMapper.getWindwarningByID(id);
    }

    @Override
    public List<WindGrade> getWindGrade() {
        return windInformationMapper.getWindGrade();
    }

    @Override
    public List<WindWarning> getWindWarning() {
        return windInformationMapper.getWindwarning();
    }

}
